$(document).ready(function() {
    $('#wiki').on("click", function() {
        callTheInfo();
    });
    $(document).keypress(function(e) {
        if(e.which == 13) {
            callTheInfo();
        }
    });
    function callTheInfo(){
        $.ajax({
            url: 'https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=' + $('#custom-input').val(),
            dataType: 'jsonp',
            type: 'POST',
            headers: {
                'Api-User-Agent': 'Example/1.0'
            },
            success: function(data) {
                if(data.error){
                    $('.error-container').html(data.error.info);
                    $('.output-container').css('display','none');
                }
                else{
                    $('.error-container').html('');
                    $('.output-container').css('display','none');
                    if (data.query.search.length === 0){
                        $('.error-container').html('No search result ((( Try something else!');
                    }
                    else{
                        $('.output-container').html('');
                        $('.output-container').css('display', 'block');
                        var heading = '';
                        var description = '';
                        for (var i = 0; i < data.query.search.length; i++){
                            heading = '<div class="add-title">' + data.query.search[i].title + '</div>';
                            description = '<div class="add-descr">' + data.query.search[i].snippet + '</div>';
                            $('.output-container').css('display', 'block');
                            var linkOpen = '<a href="https://en.wikipedia.org/wiki/' + data.query.search[i].title + '" target="_blank">';
                            var linkClose = '</a>'
                            $('.output-container').append(linkOpen + heading + description + '<hr>' + linkClose);
                        }
                    }
                }
            }
        });
    };
});